package test1;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map.Entry;

public class jjjjj {
	static int tNum;
	static int sNum;
	static int t_sNum;
	static HashMap<String, ArrayList<stu>> map;

	static ArrayList<stu> surplus_slist;

	public static void main(String[] args) {
		// 老师列表
		ArrayList<String> tlist = new ArrayList<String>();
		tlist.add("t1");
		tlist.add("t2");
		tlist.add("t3");
		tlist.add("t4");

		// 学生列表
		ArrayList<stu> slist = new ArrayList<stu>();
		stu s1 = new stu();
		s1.setSid(1);
		s1.setTids("t1,t3,t2");
		slist.add(s1);

		stu s2 = new stu();
		s2.setSid(2);
		s2.setTids("t4,t3,t1");
		slist.add(s2);

		stu s3 = new stu();
		s3.setSid(3);
		s3.setTids("t4,t3,t2");
		slist.add(s3);

		stu s4 = new stu();
		s4.setSid(4);
		s4.setTids("t4,t1,t2");
		slist.add(s4);

		stu s5 = new stu();
		s5.setSid(5);
		s5.setTids("t1,t3,t4");
		slist.add(s5);

		stu s6 = new stu();
		s6.setSid(6);
		s6.setTids("t2,t1,t4");
		slist.add(s6);

		stu s7 = new stu();
		s7.setSid(7);
		s7.setTids("t1,t3,t2");
		slist.add(s7);

		stu s8 = new stu();
		s8.setSid(8);
		s8.setTids("t2,t3,t1");
		slist.add(s8);

		stu s9 = new stu();
		s9.setSid(9);
		s9.setTids("t3,t1,t2");
		slist.add(s9);

		stu s10 = new stu();
		s10.setSid(10);
		s10.setTids("t4,t3,t2");
		slist.add(s10);

		stu s11 = new stu();
		s11.setSid(11);
		s11.setTids("t1,t4,t2");
		slist.add(s11);

		stu s12 = new stu();
		s12.setSid(12);
		s12.setTids("t2,t1,t4");
		slist.add(s12);

		surplus_slist = new ArrayList<stu>(slist);

		f1(tlist, slist);
		f2(surplus_slist);

		for (Entry<String, ArrayList<stu>> entry : map.entrySet()) {
			System.out.println(entry.getKey());
			for (stu sl : entry.getValue()) {
				System.out.println(sl.getSid());
			}
		}
		System.out.println("结束！");
	}

	private static void f1(ArrayList<String> tlist, ArrayList<stu> slist) {
		tNum = tlist.size();
		sNum = slist.size();
		t_sNum = sNum / tNum;

		map = new HashMap<String, ArrayList<stu>>();
		for (int i = 1; i < tNum + 1; i++) {
			map.put("t" + i, new ArrayList<stu>());
		}

		Collections.shuffle(slist);
		for (stu sl : slist) {
			String[] tid = sl.getTids().split(",");
			for (int i = 0; i < tid.length; i++) {
				if (y1(tid[i], sl)) {
					break;
				}
			}
		}
	}

	private static boolean y1(String tid, stu stu) {
		for (Entry<String, ArrayList<stu>> entry : map.entrySet()) {
			if (tid.equals(entry.getKey()) && entry.getValue().size() <= t_sNum - 1) {
				map.get(tid).add(stu);
				surplus_slist.remove(stu);
				return true;
			}
		}
		return false;
	}

	private static void f2(ArrayList<stu> surplus_slist2) {
		for (Entry<String, ArrayList<stu>> entry : map.entrySet()) {
			if (entry.getValue().size() >= t_sNum) {
				continue;
			}
			while (entry.getValue().size() < t_sNum) {
				map.get(entry.getKey()).add(surplus_slist2.get(0));
				surplus_slist2.remove(0);
			}
		}
	}
}
